<template>
	<!-- 使用lrz进行上传图片压缩 -->
	<div>
		<el-upload action="#" list-type="picture-card" :auto-upload="false" :on-preview="handlePictureCardPreview"
			:limit="2" :file-list="fileList" :on-change="handleChange" :on-remove="handleRemove">
			<i class="el-icon-plus"></i>
			<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过65kb，超过65kb将会被压缩</div>
		</el-upload>
		<!-- 图片放大-->
		<el-dialog :visible.sync="dialogVisible" :append-to-body='true' @close='dialogVisible=false'>
			<img width="100%" :src="dialogImageUrl" alt="">
		</el-dialog>
	</div>
</template>

<script>
	import lrzCompress from "./lrz/lrz.js"
	export default {
		provide: {
			name: '我是provide'
		},
		data() {
			return {
				dialogVisible: false, // 弹框的出现与否
				// 压缩图片
				fileList: [],
				dialogImageUrl: '',
				// 表单
				formData: {
					file: null,
				},
			}
		},
		created() {
			const a1 = [1, 2];
			// const a2 = a1.concat();
			const a2 = [...a1];
			a2[0] = 2;
			console.log(a1)
			const arr1 = ['a', 'b'];
			const arr2 = ['c'];
			const arr3 = ['d', 'e'];
			
			// ES5 的合并数组
			let a=arr1.concat(arr2, arr3);
			// [ 'a', 'b', 'c', 'd', 'e' ]
			
			// ES6 的合并数组
			let b=[...arr1, ...arr2, ...arr3]
			// [ 'a', 'b', 'c', 'd', 'e' ]
			b[0]=12
			console.log(arr1)
			console.log(a)
			console.log(b)
		},
		methods: {
			// 表单提交之前的文件校验
			beforeAvatarUpload(file) {
				const isImg = file.type === 'image/jpeg' || file.type === 'image/png'
				const isLt65K = file.size / 1024 < 65
				console.log(file.size)
				if (!isImg) {
					this.$message.error('上传只能是 JPG 或 PNG 格式!')
				}
				if (!isLt65K) {
					this.$message.error('上传大小不能超过 65k!')
				}
				return isImg && isLt65K
			},
			// 移除文件操作
			handleRemove(file, fileList) {
				this.fileList = []
				this.formData.file = null
			},
			// 图片预览
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url
				this.dialogVisible = true
			},
			// 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
			handleChange(file, fileList) {
				this.fileList = []
				if (!(fileList.length > 0)) {
					this.formData.file = null
					return
				}
				this.fileList.push(file)
				if (file.size / 1024 <= 65) {
					this.formData.file = file.raw
					this.isUpdateFile = true
					return
				}
				// 如果文件大于65kb，将文件进行压缩
				lrzCompress(file.raw, 300).then((data) => {
					console.log(data);
					this.formData.file = data.file
					this.isUpdateFile = true
				}).catch(() => {
					this.$message.error('logo压缩失败，请将logo自行压缩后上传!')
				})
			},

		}
	}
</script>
<style scoped>
	
</style>
